#include <windows.h>
#include <iostream>
#include <vector>
#include <string>

const std::string get_temp_file_name_a() {
    CHAR tempPath[MAX_PATH];
    CHAR tempFileName[MAX_PATH];
    
    // 获取系统临时目录
    GetTempPathA(MAX_PATH, tempPath);
    
    // 生成临时文件名
    UINT result = GetTempFileNameA(
        tempPath,           // 目录路径
        "YHDN",            // 前缀
        0,                 // 0表示使用随机数
        tempFileName       // 输出缓冲区
    );
    
    return tempFileName;
}

int main(int argc ,char** argv) {
    const std::vector<std::string> m_files = {"d:\\pdf files\\t3.pdf", "d:\\pdf files\\t2.pdf", "d:\\pdf files\\t1.pdf"};
    std::string cmdLine = "pdftk.exe ";

    for (auto file : m_files) {
        cmdLine += "\"";
        cmdLine += file;
        cmdLine += "\" ";
    }

    auto tmpFileA = get_temp_file_name_a();
    cmdLine = cmdLine + "cat output \"" + tmpFileA + "\"";

    std::cout << cmdLine << std::endl;
    
    STARTUPINFOA si = { sizeof(si) };
    PROCESS_INFORMATION pi = { 0 };
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
    
    char* cmd = _strdup(cmdLine.c_str());
    CreateProcessA(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
    free(cmd);    
    WaitForSingleObject(pi.hProcess, INFINITE);    
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    // 打开临时文件
    std::string cmdLine2 = std::string("AcroRd32.exe ") + "\"" + tmpFileA + "\"";   
    std::cout << cmdLine2 << std::endl;
    system(cmdLine2.c_str());

    return 0;
}